from tkinter import messagebox
from datetime import datetime
from .logic_temp import save_tasks
from .ui_temp import create_card



def add_task(task_name, priority, due_date, image_path, dialog):
    try:
        if not task_name or not priority or not due_date:
            raise ValueError("you must enter all")
        datetime.strptime(due_date, '%Y-%m-%d')  
        create_card(frames["To_Do"], task_name, priority, due_date, image_path)
        dialog.destroy()
    except ValueError as ve:
        messagebox.showerror("error", str(ve))
    except Exception as e:
        messagebox.showerror("unknown error", str(e))
        create_card(frames["To_Do"], task_name, priority, due_date, image_path)
        dialog.destroy()
        save_tasks()  # 保存任务数据到JSON文件131233333333333

def create_cards_from_data(data):
    try:
        for task in data:
            create_card(frames["To_Do"], task['name'], task['priority'], task['due_date'], task['image_path'])
    except Exception as e:
        messagebox.showerror("Error creating cards", str(e))

